package com.hs.app.action.search;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hs.app.service.ISearchService;
import com.hs.core.action.BaseAction;
import com.hs.core.cache.DictCacheUtil;
import com.hs.core.db.DBMgr;
import com.hs.core.db.QueryPageMysql;
@Controller  
public class SearchController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(SearchController.class);
	private static Map CONSTANTMAP = new HashMap();
	private ISearchService searchService;
	@Autowired  
	public void setSearchService(ISearchService searchService) {
		this.searchService = searchService;
	} 
	 /**
	   * 简单医案检索
	   * @param request
	   * @param response
	   * @param modelMap
	   * @return
	   * @throws ServletException
	   * @throws IOException
	   */
	   @RequestMapping(value="/search/searchindex.do")   
	   public ModelAndView searchIndex(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		     String dname = request.getParameter("dname");
		     String xuewei = request.getParameter("xuewei");
		     String zhichengfj = request.getParameter("zhichengfj");
		     String xingyins = request.getParameter("xingyins");
		     String geiyaofs = request.getParameter("geiyaofs");
		     String disname = request.getParameter("disname");
		     String herbname = request.getParameter("herbname");
		     
		      
		     if(StringUtils.isNotEmpty(dname))
		    	 modelMap.put("dname", dname);
		     if(StringUtils.isNotEmpty(xuewei))
		    	 modelMap.put("xuewei", xuewei);
		     if(StringUtils.isNotEmpty(zhichengfj))
		    	 modelMap.put("zhichengfj", zhichengfj);
		     if(StringUtils.isNotEmpty(xingyins))
		    	 modelMap.put("xingyins", xingyins);
		     if(StringUtils.isNotEmpty(geiyaofs))
		    	 modelMap.put("geiyaofs", geiyaofs);
		     if(StringUtils.isNotEmpty(disname))
		    	 modelMap.put("disname", disname);
		     if(StringUtils.isNotEmpty(herbname))
		    	 modelMap.put("herbname", herbname);
			 return new ModelAndView("app/search/searchIndex",modelMap);
			 
	   } 
	 
	  /**
	   * 简单医案检索
	   * @param request
	   * @param response
	   * @param modelMap
	   * @return
	   * @throws ServletException
	   * @throws IOException
	   */
	   @RequestMapping(value="/search/simplecasesearch.do")   
	   public ModelAndView simplecasesearch(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
			 Map paramMap = generateSimplesql(request); 
 
			 QueryPageMysql qp = new QueryPageMysql(request,request.getContextPath()+"/search/simplecasesearch.do",paramMap.get("allsql")+"",paramMap); 
	//	     
		     qp.query();  
		     String pagenum =  qp.getCurrentPageNo() + "";
		     String pagecount = qp.getPerPageCount() + "";
		     modelMap.put("pagenum", pagenum);
		     modelMap.put("pagecount", pagecount);
		     modelMap.put("pagestr", qp.getPageStr());
		     modelMap.put("totalcount", qp.totalCount+"");
		     modelMap.put("datalist", qp.getDataList()); 
		     modelMap.put("keyword", (String)paramMap.get("keyword"));  
			 return new ModelAndView("app/search/caselist",modelMap);
			 
		 } 
	   
	   private Map generateSimplesql(HttpServletRequest request){
		     Map paramMap = new HashMap();
		     String q = request.getParameter("q"); 
		     String qtype = request.getParameter("qtype"); 
			 String keyword = null;
			 StringBuffer allfullsql = new StringBuffer("select a.* from cfcd_index a "); 
			 String nianfen = request.getParameter("nianfen");
			 String nianlingfz = request.getParameter("huanzhenlfz");
			 String mlzydj = request.getParameter("mlzydj");
			 String zhichengfjfz = request.getParameter("zhichengfjfz");
			 String xingyinsfz = request.getParameter("xingyinsfz");
			 logger.debug("q:" + q);
			 logger.debug("nianfen:" +nianfen);
			 logger.debug("qtype:" + qtype);
			 if(StringUtils.isNotEmpty(q)){
				 if(StringUtils.isNotEmpty(qtype)&&!"null".equals(qtype)){
				
					if("zhichengfj".equals(qtype)){
						allfullsql.append(" ,yishengqk b where a.paperid=b.wenxianqkid ");
						if(StringUtils.isNotEmpty(nianfen)){
							allfullsql.append(" and a.nian = ").append(nianfen);
						}
						if(StringUtils.isNotEmpty(nianlingfz)){
							allfullsql.append(" and a.huanzhenlfz = ").append(nianlingfz);
						}
						if(StringUtils.isNotEmpty(mlzydj)){
							allfullsql.append(" and a.mlzydj = ").append(mlzydj);
						}
						if(StringUtils.isNotEmpty(zhichengfjfz)){
							allfullsql.append(" and a.zhichengfj = ").append(zhichengfjfz);
						}
						if(StringUtils.isNotEmpty(xingyinsfz)){
							allfullsql.append(" and a.xingyins = ").append(xingyinsfz);
						}
						allfullsql.append(" and b.zhichengfj=(select ckey from ngeanconstant where cvalue='"+q+"' and fname='zhichengfj')"); 
					}
					if("xingyins".equals(qtype)){
						allfullsql.append(" ,yishengqk b where a.paperid=b.wenxianqkid "); 
						if(StringUtils.isNotEmpty(nianfen)){
							allfullsql.append(" and a.nian = ").append(nianfen);
						}
						if(StringUtils.isNotEmpty(nianlingfz)){
							allfullsql.append(" and a.huanzhenlfz = ").append(nianlingfz);
						}
						if(StringUtils.isNotEmpty(mlzydj)){
							allfullsql.append(" and a.mlzydj = ").append(mlzydj);
						}
						if(StringUtils.isNotEmpty(zhichengfjfz)){
							allfullsql.append(" and a.zhichengfj = ").append(zhichengfjfz);
						}
						if(StringUtils.isNotEmpty(xingyinsfz)){
							allfullsql.append(" and a.xingyins = ").append(xingyinsfz);
						}
						allfullsql.append("and b.xingyins=(select ckey from ngeanconstant where cvalue='"+q+"' and fname='xingyins')");

					}
					if("geiyaofs".equals(qtype)){
						allfullsql.append(" where 1=1");
						if(StringUtils.isNotEmpty(nianfen)){
							allfullsql.append(" and a.nian = ").append(nianfen);
						}
						if(StringUtils.isNotEmpty(nianlingfz)){
							allfullsql.append(" and a.huanzhenlfz = ").append(nianlingfz);
						}
						if(StringUtils.isNotEmpty(mlzydj)){
							allfullsql.append(" and a.mlzydj = ").append(mlzydj);
						}
						if(StringUtils.isNotEmpty(zhichengfjfz)){
							allfullsql.append(" and a.zhichengfj = ").append(zhichengfjfz);
						}
						if(StringUtils.isNotEmpty(xingyinsfz)){
							allfullsql.append(" and a.xingyins = ").append(xingyinsfz);
						}
						allfullsql.append(" and id in(select  binganxxid from zhenci where id in(select zhenciid from zhongyaozl where geiyaofs='").append(q).append("'))");
						
					}
				 }else{
					 allfullsql.append(" where ");
					 allfullsql.append(" (instr(huanzhexm,'").append(q).append("')>0 ");//患者姓名
					 allfullsql.append(" or instr(wenti,'").append(q).append("')>0 ");//标题
					 allfullsql.append(" or instr(zhenghou,'").append(q).append("')>0 ");//证候
					 allfullsql.append(" or instr(zhengzhuang,'").append(q).append("')>0 ");//症状
					 allfullsql.append(" or instr(yaofangmc,'").append(q).append("')>0 ");//药方名称
					 allfullsql.append(" or instr(xuewei,'").append(q).append("')>0 ");//穴位
					 allfullsql.append(" or instr(danweiymc,'").append(q).append("')>0 ");//单味药名称
					 allfullsql.append(" or instr(dname,'").append(q).append("')>0 ");//医生姓名
					 allfullsql.append(" or instr(zuozhexm,'").append(q).append("')>0 ");//文献作者
					 allfullsql.append(" or instr(zhongyijbmc,'").append(q).append("')>0 ");//中医疾病名称
					 allfullsql.append(" or instr(xiyijbmc,'").append(q).append("'))>0 ");//西医疾病名称
					 if(StringUtils.isNotEmpty(nianfen)){
							allfullsql.append(" and a.nian = ").append(nianfen);
						}
						if(StringUtils.isNotEmpty(nianlingfz)){
							allfullsql.append(" and a.huanzhenlfz = ").append(nianlingfz);
						}
						if(StringUtils.isNotEmpty(mlzydj)){
							allfullsql.append(" and a.mlzydj = ").append(mlzydj);
						}
						if(StringUtils.isNotEmpty(zhichengfjfz)){
							allfullsql.append(" and a.zhichengfj = ").append(zhichengfjfz);
						}
						if(StringUtils.isNotEmpty(xingyinsfz)){
							allfullsql.append(" and a.xingyins = ").append(xingyinsfz);
						}
					 keyword = q;
				 }
	         }else{
	        	 allfullsql.append(" where  1=1 ");
	        	 if(StringUtils.isNotEmpty(nianfen)){
						allfullsql.append(" and a.nian = ").append(nianfen);
					}
					if(StringUtils.isNotEmpty(nianlingfz)){
						allfullsql.append(" and a.huanzhenlfz = ").append(nianlingfz);
					}
					if(StringUtils.isNotEmpty(mlzydj)){
						allfullsql.append(" and a.mlzydj = ").append(mlzydj);
					}
					if(StringUtils.isNotEmpty(zhichengfjfz)){
						allfullsql.append(" and a.zhichengfj = ").append(zhichengfjfz);
					}
					if(StringUtils.isNotEmpty(xingyinsfz)){
						allfullsql.append(" and a.xingyins = ").append(xingyinsfz);
					}
	         }
			 logger.debug("allfullsql:"  +allfullsql );
			 paramMap.put("allsql", allfullsql.toString());
			 paramMap.put("keyword",keyword);
			  
			 paramMap.put("nianfen",nianfen);
			 paramMap.put("huanzhenlfz",nianlingfz);
			 paramMap.put("mlzydj",mlzydj);
			 paramMap.put("zhichengfjfz",zhichengfjfz);
			 paramMap.put("xingyinsfz",xingyinsfz);
		   return paramMap;
	   }
	   /**
	    * 按文献发表年份分组
	    * @param request
	    * @param response
	    * @param modelMap
	    * @throws ServletException
	    * @throws IOException
	    */
	    @RequestMapping(value="/search/groupbyYear.do")   
	    public void groupbyYear(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
	    	 StringBuffer contentBuffer = new StringBuffer();
	    	 String searchtype = request.getParameter("searchtype");
	    	 
	    	 Map pmap = new HashMap();
	    	 if("2".equals(searchtype)){
	    		 pmap = generateQuerysql(request);
	    	 }else{
	    		 pmap = generateSimplesql(request);
	    	 }
	    	 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
			 String sql = "select b.nian,count(*) as cnum from (" + pmap.get("allsql") + ") b group by b.nian ";
			 List groupList = dbo.queryList(sql, null);
			 for(int i =0;i<groupList.size();i++){
				 Map data = (Map)groupList.get(i);
				 String nianfen = data.get("nian") + "";
				 String cnumstr = data.get("cnum") + "";
				 contentBuffer.append(" <a href=\"javaScript:groupSearch('"+request.getContextPath()+"','&nianfen="+nianfen+"');\">");
				 contentBuffer.append(nianfen);
				 contentBuffer.append("<span>(");
				 contentBuffer.append(cnumstr);
				 contentBuffer.append(")</span></a>\n");
			 }
			 logger.debug("sql:" + sql);
			 logger.debug("contentBuffer:" + contentBuffer);
	         PrintWriter out = response.getWriter();
		     out.print(contentBuffer.toString());
		     logger.debug("contentBuffer:" + contentBuffer.toString());
		     out.flush();
		     out.close();
		 } 
	     /**
		    * 按行医年数分组
		    * @param request
		    * @param response
		    * @param modelMap
		    * @throws ServletException
		    * @throws IOException
		    */
		    @RequestMapping(value="/search/groupbyXingyins.do")   
		    public void groupbyXingyins(HttpServletRequest request,
						HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		    	 StringBuffer contentBuffer = new StringBuffer();
		    	 DictCacheUtil cache = DictCacheUtil.getInst();
		    	 String searchtype = request.getParameter("searchtype");
		    	 Map pmap = new HashMap();
		    	 if("2".equals(searchtype)){
		    		 pmap = generateQuerysql(request);
		    	 }else{
		    		 pmap = generateSimplesql(request);
		    	 }
		    	 
		    	 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
				 String sql = "select xingyins,count(*) as cnum from (" + pmap.get("allsql") + ") b  group by xingyins ";
				 List groupList = dbo.queryList(sql, null);
				 for(int i =0;i<groupList.size();i++){
					 Map data = (Map)groupList.get(i);
					 String xingyins = cache.getConstantNameByCode(data.get("xingyins")+"","xingyins");
					 String cnumstr = data.get("cnum") + "";
					 contentBuffer.append(" <a href=\"javaScript:groupSearch('"+request.getContextPath()+"','&xingyinsfz="+data.get("xingyins")+"');\">");
					 contentBuffer.append(xingyins);
					 contentBuffer.append("<span>(");
					 contentBuffer.append(cnumstr);
					 contentBuffer.append(")</span></a>\n");
				 }
				 logger.debug("groupbyMlzydj:" + sql);
		         PrintWriter out = response.getWriter();
			     out.print(contentBuffer.toString());
			     logger.debug("contentBuffer:" + contentBuffer.toString());
			     out.flush();
			     out.close();
			 } 
		    /**
			    * 按名老中医等级分组
			    * @param request
			    * @param response
			    * @param modelMap
			    * @throws ServletException
			    * @throws IOException
			    */
			    @RequestMapping(value="/search/groupbyMlzydj.do")   
			    public void groupbyMlzydj(HttpServletRequest request,
							HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
			    	 StringBuffer contentBuffer = new StringBuffer();
			    	 DictCacheUtil cache = DictCacheUtil.getInst();
			    	 String searchtype = request.getParameter("searchtype");
			    	 Map pmap = new HashMap();
			    	 if("2".equals(searchtype)){
			    		 pmap = generateQuerysql(request);
			    	 }else{
			    		 pmap = generateSimplesql(request);
			    	 }
			    	 
			    	 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
					 String sql = "select mlzydj,count(*) as cnum from (" + pmap.get("allsql") + ") b group by mlzydj ";
					 List groupList = dbo.queryList(sql, null);
					 for(int i =0;i<groupList.size();i++){
						 Map data = (Map)groupList.get(i);
						 String laozhongydj = cache.getConstantNameByCode(data.get("mlzydj")+"","laozhongydj");
						 String cnumstr = data.get("cnum") + "";
						 contentBuffer.append(" <a href=\"javaScript:groupSearch('"+request.getContextPath()+"','&mlzydj="+data.get("mlzydj")+"');\">");

						 contentBuffer.append(laozhongydj);
						 contentBuffer.append("<span>(");
						 contentBuffer.append(cnumstr);
						 contentBuffer.append(")</span></a>\n");
					 }
					 logger.debug("groupbyMlzydj:" + sql);
			         PrintWriter out = response.getWriter();
				     out.print(contentBuffer.toString());
				     logger.debug("contentBuffer:" + contentBuffer.toString());
				     out.flush();
				     out.close();
				 } 
			    /**
				    * 按患者年龄分组
				    * @param request
				    * @param response
				    * @param modelMap
				    * @throws ServletException
				    * @throws IOException
				    */
				    @RequestMapping(value="/search/groupbyHuanzhenl.do")   
				    public void groupbyHuanzhenl(HttpServletRequest request,
								HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
				    	 StringBuffer contentBuffer = new StringBuffer();
				    	 String searchtype = request.getParameter("searchtype");
				    	 Map pmap = new HashMap();
				    	 if("2".equals(searchtype)){
				    		 pmap = generateQuerysql(request);
				    	 }else{
				    		 pmap = generateSimplesql(request);
				    	 }
				    	 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
						 String sql = "select b.huanzhenlfz,count(*) as cnum from (" + pmap.get("allsql") + ") b group by b.huanzhenlfz ";
						 List groupList = dbo.queryList(sql, null);
						 for(int i =0;i<groupList.size();i++){
							 
							 Map data = (Map)groupList.get(i);
							 String nianling = data.get("huanzhenlfz") + "";
//							 婴儿（0-2） 1 
//							 儿童，学龄前（3-5）  2
//							 儿童（6-12） 3
//							 青年人（13-18） 4
//							 成年人（19-44） 5
//							 中年人（45-64） 6
//							 老年人（65-79） 7
//							 老年人（>=80）8
							 if("1".equals(nianling)){
								 nianling = "婴儿";
							 }else if("2".equals(nianling)){
								 nianling = "儿童，学龄前";
							 }else if("3".equals(nianling)){
								 nianling = "儿童";
							 }else if("4".equals(nianling)){
								 nianling = "青年人";
							 }else if("5".equals(nianling)){
								 nianling = "成年人";
							 }else if("6".equals(nianling)){
								 nianling = "中年人";
							 }else if("7".equals(nianling)){
								 nianling = "老年人";
							 }else if("8".equals(nianling)){
								 nianling = "80岁以上老年人";
							 }
							 String cnumstr = data.get("cnum") + "";
							 contentBuffer.append(" <a href=\"javaScript:groupSearch('"+request.getContextPath()+"','&huanzhenlfz="+data.get("huanzhenlfz")+"');\">");
							 contentBuffer.append(nianling);
							 contentBuffer.append("<span>(");
							 contentBuffer.append(cnumstr);
							 contentBuffer.append(")</span></a>\n");
						 }
						 logger.debug("sql:" + sql);
				         PrintWriter out = response.getWriter();
					     out.print(contentBuffer.toString());
					     logger.debug("contentBuffer:" + contentBuffer.toString());
					     out.flush();
					     out.close();
					 } 
		    /**
			    * 按职称分级分组
			    * @param request
			    * @param response
			    * @param modelMap
			    * @throws ServletException
			    * @throws IOException
			    */
			    @RequestMapping(value="/search/groupbyZhichengfj.do")   
			    public void groupbyZhichengfj(HttpServletRequest request,
							HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
			    	 StringBuffer contentBuffer = new StringBuffer();
			    	 String searchtype = request.getParameter("searchtype");
			    	 DictCacheUtil cache = DictCacheUtil.getInst();
			    	 Map pmap = new HashMap();
			    	 if("2".equals(searchtype)){
			    		 pmap = generateQuerysql(request);
			    	 }else{
			    		 pmap = generateSimplesql(request);
			    	 }
			    	 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
					 String sql = "select zhichengfj,count(*) as cnum from (" + pmap.get("allsql") + ") b group by zhichengfj ";
					 List groupList = dbo.queryList(sql, null);
					 for(int i =0;i<groupList.size();i++){
						 Map data = (Map)groupList.get(i);
						 String zhichengfj = cache.getConstantNameByCode(data.get("zhichengfj")+"","zhichengfj");
						 String cnumstr = data.get("cnum") + "";
						 contentBuffer.append(" <a href=\"javaScript:groupSearch('"+request.getContextPath()+"','&zhichengfjfz="+data.get("zhichengfj")+"');\">");
						 contentBuffer.append(zhichengfj);
						 contentBuffer.append("<span>(");
						 contentBuffer.append(cnumstr);
						 contentBuffer.append(")</span></a>\n");
					 }
					 logger.debug("sql:" + sql);
			         PrintWriter out = response.getWriter();
				     out.print(contentBuffer.toString());
				     logger.debug("contentBuffer:" + contentBuffer.toString());
				     out.flush();
				     out.close();
				 } 
	 /**
	  * 高级医案检索
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/complexcasesearch.do")   
     public ModelAndView complexcasesearch(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 
		 Map pmap = generateQuerysql(request);
		 String keyword = null;
 		 String sqlStr = (String)pmap.get("allsql");
		 String keywordStr = (String)pmap.get("keyword");
		 if(StringUtils.isNotEmpty(keywordStr)){
			 keyword = keywordStr;
		 }
		 pmap.put("keyword", keyword);  
         logger.debug("allfullsql:" + sqlStr);
		 QueryPageMysql qp = new QueryPageMysql(request,request.getContextPath()+"/search/complexcasesearch.do",sqlStr,pmap); 
//	     
	     qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + "";
	     modelMap.put("pagenum", pagenum);
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList()); 
	     modelMap.put("keyword", keyword); 
	     modelMap.put("totalcount", qp.totalCount+"");
	     Log.debug("keyword:" + keyword);
	     modelMap.put("allfullsql", sqlStr); 
	     
		 return new ModelAndView("app/search/caselist",modelMap);
		 
	 } 
	 private Map generateQuerysql(HttpServletRequest request){
		 Map paramsmap = new HashMap();
		 StringBuffer keywords = new StringBuffer();
		 String dname = request.getParameter("dname"); 
		 String zhenghou = request.getParameter("zhenghou"); 
		 String zhengzhuang = request.getParameter("zhengzhuang"); 
		 String yaofangmc = request.getParameter("yaofangmc"); 
		 String xueweimc = request.getParameter("xueweimc"); 
		 String herbname = request.getParameter("herbname"); 
		 String disname = request.getParameter("disname"); 
		 String wenti = request.getParameter("wenti"); 
		 String author = request.getParameter("author"); 
		 
		 String clevel = request.getParameter("clevel"); 
		 String plevel = request.getParameter("plevel"); 
		 String flevel = request.getParameter("flevel"); 
		 
		 String iszy = request.getParameter("iszy"); 
		 String isxy = request.getParameter("isxy"); 
		 String isam = request.getParameter("isam"); 
		 String iszj = request.getParameter("iszj"); 
		 
		 String nianfen = request.getParameter("nianfen");
		 String nianlingfz = request.getParameter("huanzhenlfz");
		 String mlzydj = request.getParameter("mlzydj");
		 String zhichengfjfz = request.getParameter("zhichengfjfz");
		 String xingyinsfz = request.getParameter("xingyinsfz");
		 
		 paramsmap.put("dname", dname);
		 paramsmap.put("zhenghou",zhenghou);
		 paramsmap.put("zhengzhuang", zhengzhuang);
		 paramsmap.put("yaofangmc",yaofangmc);
		 paramsmap.put("xueweimc", xueweimc);
		 paramsmap.put("herbname", herbname);
		 paramsmap.put("disname", disname);
		 paramsmap.put("wenti", wenti);
		 paramsmap.put("author", author);
		 
		 paramsmap.put("nianfen",nianfen);
		 paramsmap.put("huanzhenlfz",nianlingfz);
		 paramsmap.put("mlzydj",mlzydj);
		 paramsmap.put("zhichengfjfz",zhichengfjfz);
		 paramsmap.put("xingyinsfz",xingyinsfz);
		 paramsmap.put("clevel", clevel);
		 paramsmap.put("plevel",plevel);
		 paramsmap.put("flevel", flevel);
		 paramsmap.put("iszy",iszy);
		 paramsmap.put("isxy", isxy);
		 paramsmap.put("isam", isam);
		 paramsmap.put("iszj", iszj);
		 StringBuffer querySql = new StringBuffer("select * from cfcd_index a where 1=1");
		 
		 if(StringUtils.isNotEmpty(dname)){
			 querySql.append(" and ");
			 querySql.append(" EXISTS (select 1 from yishengqk c where INSTR(c.xingming,'").append(dname).append("')>0 and c.wenxianqkid=a.paperid)");
			 keywords.append(dname).append("|");
		 }
		 
		 if(StringUtils.isNotEmpty(zhenghou)){
			 querySql.append(" and ");
			 querySql.append(" instr(a.zhenghou,'").append(zhenghou).append("')>0");
			 keywords.append(zhenghou).append("|");
		 }
		 if(StringUtils.isNotEmpty(zhengzhuang)){
			 querySql.append(" and ");
			 querySql.append(" instr(a.zhengzhuang,'").append(zhengzhuang).append("')>0");
			 keywords.append(zhengzhuang).append("|");
		 }
         if(StringUtils.isNotEmpty(yaofangmc)){
        	 querySql.append(" and ");
			 querySql.append(" instr(a.yaofangmc,'").append(yaofangmc).append("')>0");
			 keywords.append(yaofangmc).append("|");
		 }
         if(StringUtils.isNotEmpty(xueweimc)){
        	 querySql.append(" and ");
			 querySql.append(" instr(a.xuewei,'").append(xueweimc).append("')>0");
			 keywords.append(xueweimc).append("|");
		 }
		 if(StringUtils.isNotEmpty(herbname)){
			 querySql.append(" and ");
			 querySql.append(" instr(a.danweiymc,'").append(herbname).append("')>0");
			 keywords.append(herbname).append("|");
		 }
		 if(StringUtils.isNotEmpty(clevel)){
			 querySql.append(" and ");
			 querySql.append(" a.mlzydj=1");
			  
		 }
		 if(StringUtils.isNotEmpty(plevel)){
			 querySql.append(" and ");
			 querySql.append(" a.mlzydj=2");
		 }
		 if(StringUtils.isNotEmpty(flevel)){
			 querySql.append(" and ");
			 querySql.append(" a.mlzydj=3");
		 }
//		 if(StringUtils.isNotEmpty(disname)){
//			 querySql.append(" and ");
//			 querySql.append(" instr(a.zhongyijbmc,'").append(disname).append("')>0");
//			 querySql.append(" and ");
//			 querySql.append(" instr(a.xiyijbmc,'").append(disname).append("')>0");
//			 keywords.append(disname).append("|");
//		 }
         if(StringUtils.isNotEmpty(wenti)){
        	 querySql.append(" and ");
			 querySql.append(" instr(a.wenti,'").append(wenti).append("')>0");
			 keywords.append(wenti).append("|");
		 }
         if(StringUtils.isNotEmpty(author)){
        	 querySql.append(" and ");
			 querySql.append(" instr(a.zuozhexm,'").append(author).append("')>0");
			 keywords.append(author).append("|");
		 }
         
         if("1".equals(iszy)){
			 querySql.append(" and ");
			 querySql.append(" a.iszy=1 ");
			 
		 }
         if("1".equals(isxy)){
			 querySql.append(" and ");
			 querySql.append(" a.isxy=1 ");
			 
		 }
         if("1".equals(isam)){
			 querySql.append(" and ");
			 querySql.append(" a.isam=1 ");
			 
		 }
         if("1".equals(iszj)){
			 querySql.append(" and ");
			 querySql.append(" a.zjzl=1 ");
			 
		 }
         if(StringUtils.isNotEmpty(disname)){
        	 disname = disname.replaceAll("，", ",");
        	 String[] disnames = disname.split(",");
        	 StringBuffer disnamebuffer = new StringBuffer();
        	 for(int i=0;i<disnames.length;i++){
        		 String temp = disnames[i];
        		 keywords.append(temp).append("|");
        		 disnamebuffer.append("  instr(disname,'").append(temp).append("')>0 ");
        		 if(i < disnames.length-1){
        			 disnamebuffer.append(" or ");
        		 }
        	 }
        	 querySql.append(" and ");
			 querySql.append(" a.id in (select binganxxid from cfcd_disnames where ").append(disnamebuffer).append(")");
         }
            if(StringUtils.isNotEmpty(nianfen)){
        	   querySql.append(" and a.nian = ").append(nianfen);
			}
			if(StringUtils.isNotEmpty(nianlingfz)){
				querySql.append(" and a.huanzhenlfz = ").append(nianlingfz);
			}
			if(StringUtils.isNotEmpty(mlzydj)){
				querySql.append(" and a.mlzydj = ").append(mlzydj);
			}
			if(StringUtils.isNotEmpty(zhichengfjfz)){
				querySql.append(" and a.zhichengfj = ").append(zhichengfjfz);
			}
			if(StringUtils.isNotEmpty(xingyinsfz)){
				querySql.append(" and a.xingyins = ").append(xingyinsfz);
			}
         
         paramsmap.put("allsql", querySql.toString());
         paramsmap.put("keyword",keywords.toString());
		 return paramsmap;
	 }
	 
	 /**
	  * 医案相信信息页面
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/casedetail.do")   
    public ModelAndView viewCasedetail(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		String caseid = request.getParameter("caseid");
		String paperid = request.getParameter("paperid");
		DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
		Map zhiliaomap = new HashMap();
		Map paperinfo = dbo.queryDataMap("select * from wenxianqk where id=?", new Object[]{paperid});
		List doctorlist  = dbo.queryList("select * from yishengqk where wenxianqkid=?", new Object[]{paperid});
		Map caseinfo   = dbo.queryDataMap("select a.*,b.iszy,b.isxy,b.isam,b.ista,b.zjzl from binganxx a,cfcd_index b where a.id=b.id and a.id=?", new Object[]{caseid});
		List zhenduanlist   = dbo.queryList("select * from zhenduanpdlx where binganxxid=?", new Object[]{caseid});
		String zhongyizl = caseinfo.get("iszy") + "";
		String xiyizl = caseinfo.get("isxy") + "";
		String amzl = caseinfo.get("isam") + "";
		String zjzl = caseinfo.get("zjzl") + "";
		String qitazl = caseinfo.get("ista") + "";
		
		StringBuffer allbuffer = new StringBuffer();
		
		if("1".equals(zhongyizl)){
			allbuffer.append(generateZhongyizl(caseid,dbo));
		}
		if("1".equals(xiyizl)){
			allbuffer.append(generateXiyizl(caseid,dbo));
	    }
		if("1".equals(amzl)){
			allbuffer.append(generateAnmozl(caseid,dbo));
		}
		if("1".equals(zjzl)){
			allbuffer.append(generateZhenjiuzl(caseid,dbo));
		}
		if("1".equals(qitazl)){
			allbuffer.append(generateQitazl(caseid,dbo));
		}
		caseinfo.put("zhenduanlist", zhenduanlist);
		modelMap.put("paperinfo", paperinfo);
		modelMap.put("doctorlist", doctorlist);
		modelMap.put("caseinfo", caseinfo);
		modelMap.put("xiyizlstr", allbuffer.toString());
		return new ModelAndView("app/search/casedetail",modelMap);
		 
	 } 
	 /**
	  * 西医治疗
	  * @param caseid
	  * @param dbo
	  * @return
	  */
	  private String generateXiyizl(String caseid,DBMgr dbo){
		  StringBuffer xiyibuffer = new StringBuffer();
		  List zhencilist = dbo.queryList("select * from zhenci where binganxxid = ? order by zhenci ", new Object[]{caseid});
		  int zhencicount = zhencilist.size();
		  for(int i = 0;i < zhencicount;i++){
			  Map zhencimap = (Map)zhencilist.get(i);
			  xiyibuffer.append("<tr>\n");
			  if(i == 0){
				  xiyibuffer.append("<td rowspan=\""+zhencicount+"\"><strong>西医治疗</strong></td>\n");
			  }
			  xiyibuffer.append("<td width=\"86\" valign=center><strong>" + DIGSTMAP.get(i+"") + "诊</strong></td>\n");
			  xiyibuffer.append("<td width=\"260\">\n");
			  xiyibuffer.append(" <strong>证候：</strong>").append(zhencimap.get("zhenghoumc")==null?"未知":zhencimap.get("zhenghoumc")).append("\n<br />");
			  xiyibuffer.append(" <strong>治法：</strong>").append(zhencimap.get("zhifa")==null?"未知":zhencimap.get("zhifa")).append("\n");
			  xiyibuffer.append("</td>\n");
			  xiyibuffer.append("<td>\n");
			  List xiyaolist = dbo.queryList("select * from xiyaozl where zhenciid=?", new Object[]{zhencimap.get("id")});
              for(int k=0;k<xiyaolist.size();k++){
            	  Map xiyaomap = (Map)xiyaolist.get(k);
            	  xiyibuffer.append("<strong>药名：</strong>").append(xiyaomap.get("xiyaomc")).append("<br />\n");
            	  xiyibuffer.append("<strong>给药方式：</strong>").append(xiyaomap.get("yaowujx")).append("<br />\n");
            	  xiyibuffer.append("<strong>给药频率：</strong>").append(xiyaomap.get("geiyaopl")).append("<br />\n");
              }
			  xiyibuffer.append("</td>\n"); 
			  xiyibuffer.append("</tr>\n");
		  } 
		  return xiyibuffer.toString();
	  }
	  /**
	   * 中医治疗
	   * @param caseid
	   * @param dbo
	   * @return
	   */
	  private String generateZhongyizl(String caseid,DBMgr dbo){
		  StringBuffer xiyibuffer = new StringBuffer();
		  List zhencilist = dbo.queryList("select * from zhenci where binganxxid = ? order by zhenci ", new Object[]{caseid});
		  int zhencicount = zhencilist.size();
		  DictCacheUtil cache = DictCacheUtil.getInst();
		  for(int i = 0;i < zhencicount;i++){
			  Map zhencimap = (Map)zhencilist.get(i);
			  xiyibuffer.append("<tr>\n");
			  if(i == 0){
				  xiyibuffer.append("<td rowspan=\""+zhencicount+"\"><strong>中医治疗</strong></td>\n");
			  }
			  xiyibuffer.append("<td width=\"86\" valign=center><strong>" + DIGSTMAP.get(i+"") + "诊</strong></td>\n");
			  xiyibuffer.append("<td width=\"260\">\n");
			  xiyibuffer.append(" <strong>证候：</strong>").append(zhencimap.get("zhenghoumc")==null?"未知":zhencimap.get("zhenghoumc")).append("\n<br />");
			  xiyibuffer.append(" <strong>治法：</strong>").append(zhencimap.get("zhifa")==null?"未知":zhencimap.get("zhifa")).append("\n<br />");
			  List zhengzhuangtz = dbo.queryList("select * from zhengzhuangtz where zhenciid=?", new Object[]{zhencimap.get("id")});
			  StringBuffer zztz = new StringBuffer();
			  for(int k=0;k<zhengzhuangtz.size();k++){
            	  Map zztzmap = (Map)zhengzhuangtz.get(k);
            	  zztz.append(zztzmap.get("dangezz"));
            	  zztz.append(zztzmap.get("bianhuaqk")==null?"":","+zztzmap.get("bianhuaqk"));
            	  zztz.append(";");
 			  }
			  xiyibuffer.append(" <strong>症状体征：</strong>").append(zztz).append("\n<br />");
			  xiyibuffer.append("</td>\n");
			  xiyibuffer.append("<td>\n");
			  List zhongyaolist = dbo.queryList("select * from zhongyaozl where zhenciid=?", new Object[]{zhencimap.get("id")});
              for(int k=0;k<zhongyaolist.size();k++){
            	  Map xiyaomap = (Map)zhongyaolist.get(k);
            	  xiyibuffer.append("<strong>药方名称：</strong>").append(xiyaomap.get("yaofangmc")).append("<br />\n");
            	  xiyibuffer.append("<strong>剂型：</strong>").append(xiyaomap.get("yaowujx")).append("<br />\n");
            	  xiyibuffer.append("<strong>给药方式：</strong>").append(xiyaomap.get("geiyaofs")).append("<br />\n");
            	  xiyibuffer.append("<strong>药方来源：</strong>").append(xiyaomap.get("yaofangly")==null?"无":xiyaomap.get("yaofangly")).append("<br />\n");
            	  List zclist = dbo.queryList("select * from zhongyaozc where zhongyaozlid=?", new Object[]{xiyaomap.get("id")});
            	  StringBuffer zyzcbuffer = new StringBuffer();
            	  for(int n =0;n<zclist.size();n++){
            		  Map zhongyaomap = (Map)zclist.get(n);
            		  zyzcbuffer.append(zhongyaomap.get("danweiymc"));
            		  if(null != zhongyaomap.get("paozhiff")){
            			  zyzcbuffer.append("(").append(zhongyaomap.get("paozhiff")).append(")");
            		  }
            		  zyzcbuffer.append(",");
            		  zyzcbuffer.append(zhongyaomap.get("jiliang"));
            		  zyzcbuffer.append(cache.getConstantNameByCode(zhongyaomap.get("jiliangdw")+"", "Jiliangdw")); 
            		  zyzcbuffer.append(zhongyaomap.get("gongxiao")==null?"":","+zhongyaomap.get("gongxiao"));
            		  zyzcbuffer.append(";");
            	  }
            	  xiyibuffer.append("<strong>组成：</strong>").append(zyzcbuffer).append("<br />\n"); 
              }
			  xiyibuffer.append("</td>\n"); 
			  xiyibuffer.append("</tr>\n");
		  } 
		  return xiyibuffer.toString();
	  }
	  /**
	   * 针灸治疗
	   * @param caseid
	   * @param dbo
	   * @return
	   */
	  private String generateZhenjiuzl(String caseid,DBMgr dbo){
		  StringBuffer xiyibuffer = new StringBuffer();
		  List zhencilist = dbo.queryList("select * from zhenci where binganxxid = ? order by zhenci ", new Object[]{caseid});
		  int zhencicount = zhencilist.size();
		  DictCacheUtil cache = DictCacheUtil.getInst();
		  for(int i = 0;i < zhencicount;i++){
			  Map zhencimap = (Map)zhencilist.get(i);
			  xiyibuffer.append("<tr>\n");
			  if(i == 0){
				  xiyibuffer.append("<td rowspan=\""+zhencicount+"\"><strong>针灸治疗</strong></td>\n");
			  }
			  xiyibuffer.append("<td width=\"86\" valign=center><strong>" + DIGSTMAP.get(i+"") + "诊</strong></td>\n");
			  xiyibuffer.append("<td width=\"260\">\n");
			  xiyibuffer.append(" <strong>证候：</strong>").append(zhencimap.get("zhenghoumc")==null?"未知":zhencimap.get("zhenghoumc")).append("\n<br />");
			  xiyibuffer.append(" <strong>治法：</strong>").append(zhencimap.get("zhifa")==null?"未知":zhencimap.get("zhifa")).append("\n");
			  xiyibuffer.append("</td>\n");
			  xiyibuffer.append("<td>\n");
			  List zhongyaolist = dbo.queryList("select * from zhenjiuzl where zhenciid=?", new Object[]{zhencimap.get("id")});
              for(int k=0;k<zhongyaolist.size();k++){
            	  Map xiyaomap = (Map)zhongyaolist.get(k);
            	  xiyibuffer.append("<strong>针灸方法：</strong>").append(xiyaomap.get("zhenjiuff")).append("<br />\n"); 
            	  List zclist = dbo.queryList("select * from zhenjiuxw where zhenjiuzlid=?", new Object[]{xiyaomap.get("id")});
            	  StringBuffer zyzcbuffer = new StringBuffer();
            	  for(int n =0;n<zclist.size();n++){
            		  Map zhongyaomap = (Map)zclist.get(n);
            		  zyzcbuffer.append(zhongyaomap.get("xuewei"));  
            		  zyzcbuffer.append(zhongyaomap.get("gongxiao")==null?"":","+zhongyaomap.get("gongxiao"));
            		  zyzcbuffer.append(";");
            	  }
            	  xiyibuffer.append("<strong>针灸穴位：</strong>").append(zyzcbuffer).append("<br />\n"); 
              }
			  xiyibuffer.append("</td>\n"); 
			  xiyibuffer.append("</tr>\n");
		  } 
		  return xiyibuffer.toString();
	  }
	  /**
	   * 按摩治疗
	   * @param caseid
	   * @param dbo
	   * @return
	   */
	  private String generateAnmozl(String caseid,DBMgr dbo){
		  StringBuffer xiyibuffer = new StringBuffer();
		  List zhencilist = dbo.queryList("select * from zhenci where binganxxid = ? order by zhenci ", new Object[]{caseid});
		  int zhencicount = zhencilist.size();
		  DictCacheUtil cache = DictCacheUtil.getInst();
		  for(int i = 0;i < zhencicount;i++){
			  Map zhencimap = (Map)zhencilist.get(i);
			  xiyibuffer.append("<tr>\n");
			  if(i == 0){
				  xiyibuffer.append("<td rowspan=\""+zhencicount+"\"><strong>按摩治疗</strong></td>\n");
			  }
			  xiyibuffer.append("<td width=\"86\" valign=center><strong>" + DIGSTMAP.get(i+"") + "诊</strong></td>\n");
			  xiyibuffer.append("<td width=\"260\">\n");
			  xiyibuffer.append(" <strong>证候：</strong>").append(zhencimap.get("zhenghoumc")==null?"未知":zhencimap.get("zhenghoumc")).append("\n");
			  xiyibuffer.append(" <strong>治法：</strong>").append(zhencimap.get("zhifa")==null?"未知":zhencimap.get("zhifa")).append("\n");
			  xiyibuffer.append("</td>\n");
			  xiyibuffer.append("<td>\n");
			  List zhongyaolist = dbo.queryList("select * from anmozl where zhenciid=?", new Object[]{zhencimap.get("id")});
              for(int k=0;k<zhongyaolist.size();k++){
            	  Map xiyaomap = (Map)zhongyaolist.get(k);
            	  xiyibuffer.append("<strong>按摩方法：</strong>").append(xiyaomap.get("anmoff")).append("<br />\n"); 
            	  List zclist = dbo.queryList("select * from anmoxw where anmozlzlid=?", new Object[]{xiyaomap.get("id")});
            	  StringBuffer zyzcbuffer = new StringBuffer();
            	  for(int n =0;n<zclist.size();n++){
            		  Map zhongyaomap = (Map)zclist.get(n);
            		  zyzcbuffer.append(zhongyaomap.get("xuewei"));  
            		  zyzcbuffer.append(zhongyaomap.get("gongxiao")==null?"":","+zhongyaomap.get("gongxiao"));
            		  zyzcbuffer.append(";");
            	  }
            	  xiyibuffer.append("<strong>按摩穴位：</strong>").append(zyzcbuffer).append("<br />\n"); 
              }
			  xiyibuffer.append("</td>\n"); 
			  xiyibuffer.append("</tr>\n");
		  } 
		  return xiyibuffer.toString();
	  }
	  /**
	   * 其它治疗
	   * @param caseid
	   * @param dbo
	   * @return
	   */
	  private String generateQitazl(String caseid,DBMgr dbo){
		  StringBuffer xiyibuffer = new StringBuffer();
		  List zhencilist = dbo.queryList("select * from zhenci where binganxxid = ? order by zhenci ", new Object[]{caseid});
		  int zhencicount = zhencilist.size();
		  DictCacheUtil cache = DictCacheUtil.getInst();
		  for(int i = 0;i < zhencicount;i++){
			  Map zhencimap = (Map)zhencilist.get(i);
			  xiyibuffer.append("<tr>\n");
			  if(i == 0){
				  xiyibuffer.append("<td rowspan=\""+zhencicount+"\"><strong>其他治疗</strong></td>\n");
			  }
			  xiyibuffer.append("<td width=\"86\" valign=center><strong>" + DIGSTMAP.get(i+"") + "诊</strong></td>\n");
			  xiyibuffer.append("<td width=\"260\">\n");
			  xiyibuffer.append(" <strong>证候：</strong>").append(zhencimap.get("zhenghoumc")==null?"未知":zhencimap.get("zhenghoumc")).append("\n");
			  xiyibuffer.append(" <strong>治法：</strong>").append(zhencimap.get("zhifa")==null?"未知":zhencimap.get("zhifa")).append("\n");
			  xiyibuffer.append("</td>\n");
			  xiyibuffer.append("<td>\n");
			  List zhongyaolist = dbo.queryList("select * from qitazl where zhenciid=?", new Object[]{zhencimap.get("id")});
              for(int k=0;k<zhongyaolist.size();k++){
            	  Map xiyaomap = (Map)zhongyaolist.get(k);
            	  xiyibuffer.append("<strong>治疗名称：</strong>").append(xiyaomap.get("zhiliaomc")).append("<br />\n");
            	  xiyibuffer.append("<strong>描述：</strong>").append(xiyaomap.get("miaoshu")).append("<br />\n"); 
            	   
              }
			  xiyibuffer.append("</td>\n"); 
			  xiyibuffer.append("</tr>\n");
		  } 
		  return xiyibuffer.toString();
	  }
	  
	  
	  static Map DIGSTMAP = new HashMap();
	  static {
		  DIGSTMAP.put("0", "一");
		  DIGSTMAP.put("1", "二");
		  DIGSTMAP.put("2", "三");
		  DIGSTMAP.put("3", "四");
		  DIGSTMAP.put("4", "五");
		  DIGSTMAP.put("5", "六");
		  DIGSTMAP.put("6", "七");
		  DIGSTMAP.put("7", "八");
		  
	  }
	  private String getSyndisname(HttpServletRequest request,String disnamestr,String paramvalue){
			 StringBuffer 	 disnamebuffer = new StringBuffer(""); 
			 Map cache = new HashMap(); 
			 DBMgr dbutil = DBMgr.getDbUtilInst(request.getSession().getServletContext()); 
			 List datalist = dbutil.queryList("select pid from SCHD_TAGS where tagname =?", new Object[]{paramvalue});
			 if(StringUtils.isNotEmpty(disnamestr)){
				 String[] temparr = disnamestr.split(",");
				 for(int i =0;i<temparr.length;i++){
					 String idstr = temparr[i];
					 cache.put(idstr, idstr);
				 }
			 } 
			 for(int i =0;i<datalist.size();i++){
				 Map data = (Map)datalist.get(i);
				 String ids = data.get("pid")+"";
				 if(null == cache.get(ids)){
					 cache.put(ids, ids);
				 }
			 }
			 Set entrySet = cache.entrySet();
			 Iterator iter = entrySet.iterator();
			 while(iter.hasNext()){
				 Entry entry = (Entry)iter.next();
				 String keyvalue = (String)entry.getValue();
				 disnamebuffer.append(keyvalue);
				 disnamebuffer.append(",");
			 }
			 String temp = null;
			 if(disnamebuffer.toString().endsWith(",")){
				 temp = disnamebuffer.substring(0, disnamebuffer.length()-1);
			 }
			 return temp;
		 }
 
//	 /**
//	  * 文献检索列表形式
//	  * @param request
//	  * @param response
//	  * @param modelMap
//	  * @return
//	  * @throws ServletException
//	  * @throws IOException
//	  */
	 @RequestMapping(value="/search/index.do")   
    public ModelAndView index(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         //DBMgr dbo = this.getDbo(request);
         //Map treedata = getTreeStr(dbo);
        // modelMap.addAllAttributes(treedata);
		 //return new ModelAndView("app/search/abstractlist",modelMap);
		 return new ModelAndView("app/search/searchIndex",modelMap);
		 
	 }  
	 private String createSql(HttpServletRequest request,String cname,StringBuffer buffer,StringBuffer keywordbuffer){
		 StringBuffer sqlbuffer = new StringBuffer();
		 //值
		 String c_value = cname+"_value";
		 //逻辑关系，并且 或者
		 String c_logical = cname+"_logical";
		 //字段的名字
		 String c_sel = cname+"_sel";
		 //精确或模糊
		 String c_special = cname+"_special";
		 
		 String cvalue = request.getParameter(c_value);
		 String clogical = request.getParameter(c_logical);
		 String csel = request.getParameter(c_sel);
		 String cspecial = request.getParameter(c_special);
		 
		 String logicalname = "";
		 
		 String colname = (String)CONSTANTMAP.get(csel);
//		 if(null != colname){
//			 if("1".equals(clogical)){
//				 logicalname = " OR ";
//				 
//			 }else{
//				 logicalname = " AND ";
//			 }
//			// buffer.append(colname ).append("“").append(cvalue).append("”&nbsp;").append(logicalname).append("&nbsp;");
//			 keywordbuffer.append(cvalue).append("|");
//		 }
		 keywordbuffer.append(cvalue).append("|");
		 request.setAttribute(c_value, cvalue);
		 request.setAttribute(c_logical, c_logical);
		 request.setAttribute(c_sel, c_sel);
		 request.setAttribute(c_special, c_special);
		 //logger.debug("value:::" + clogical + "," + csel + "," + cvalue + "," + cspecial);
//		 if(StringUtils.isEmpty(cspecial))
//			 cspecial = "1";//默认为模糊检索
		 if(null == cvalue || StringUtils.isEmpty(cvalue)){//查询条件为空时返回空
			 return "";
		 }
		 logger.debug("clogical:" + clogical);
		 logger.debug("c_sel:" + c_sel);
		 if("0".equals(clogical)){
			 //0 and 关系  1：或者关系
			 sqlbuffer.append(" and "); 
		 }else if("1".equals(clogical)){
			 //0 and 关系  1：或者关系
			 sqlbuffer.append(" or "); 
		 }else{
			 sqlbuffer.append(" and "); 
		 }
		 sqlbuffer.append("lower(").append(csel).append(")");
		 if("0".equals(cspecial)){
			 //0 精确  1：模糊
			 sqlbuffer.append(" =lower('"); 
			 sqlbuffer.append(cvalue.trim());
			 sqlbuffer.append("')");
		 }
		 if("1".equals(cspecial)){
			//0 精确  1：模糊
			 sqlbuffer.append(" like '%'||lower('"); 
			 sqlbuffer.append(cvalue.trim());
			 sqlbuffer.append("')||'%'");
		 }
		 return sqlbuffer.toString();
	 }
	  
	 /**
	  * 疾病分类树
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/distree.do")   
	   public ModelAndView distree(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 
		    StringBuilder builder = new StringBuilder();  
			DBMgr dbutil = this.getDbo(request);  
			List alldataList = dbutil.queryList("select * from cfcd_discatagory   order by id", null);
			logger.debug("alldataList:" + alldataList);
			for(int i=0;i<alldataList.size();i++){
				
				Map tag  = (Map)alldataList.get(i);
				logger.debug("map:" + tag);
				String cataname = (String)tag.get("cataname"); 
				String id =  tag.get("ID") + "";
				String pid =  tag.get("PID") + "";
		 
					builder.append("{ id:");
					builder.append(id);
					builder.append(", pId:");
					builder.append(pid);
					builder.append(", name:\"");
					builder.append(cataname);
					builder.append("\"");
					if(id.equals(pid)){
						builder.append(",chkDisabled:true"); 
					}
					builder.append("}");
					if(i != alldataList.size() - 1){
						builder.append(",");
					}
					builder.append("\n");  
				
			}
			modelMap.put("treedata", builder.toString());
	 		return new ModelAndView("app/search/distree",modelMap);
			 
	  } 
	  
	 
	  
}
